module CommentsHelper
	def display_comments(arr, parent_id=nil, level=0, class_name='red')
		comments = arr.select{ |c| c.depth == level && c.parent_id == parent_id }
		class_name2 = (class_name == 'red' ? 'blue' : 'red')
		content = ''
		comments.each do |object|
			content += content_tag('li',
			"<div class='comentPanel #{cycle class_name, class_name2}'><div class='commentHead'><div class='content'>" +
			(!object.anonymous? ? (link_to object.user.login, user_path(object.user), :class=>"user") : object.user_name) +
			"<small>(опубликовано #{object.created_at.to_s(:long)})</small>#{comment_check_box(object)}</div></div>" + pic(object.user_avatar, {:class=>"userPic"}) +
			"</div><div class='content'>#{Hpricot(object.comment_html.gsub(/\n/, "<br/>")).to_html}</div><div class='add'>" + 
			"(" + (link_to "Ответить", "#comment-form", :onclick=>"$('comment_root_id').value=#{object.id};$('div-comment-form').show();") + ")</div>" + 
			"<ul class='comment'>" + display_comments(object.direct_children, object.id, level+1, class_name) + "</ul>",
			#'',
			{:class=>(level == 1 ? "I" : "II"), :id=>"comment_#{object.id}", :style=>(level > 6 ? 'display:none;' : '')})
			content += content_tag('li', "<a href='#' onclick='show_comments(#{object.id});return false;'>показать комменты...</a>", {:id=>"show_comment_#{object.id}", :style=>(level > 6 ? '' : 'display:none;')})
		end
		content
	end
	
	def comment_check_box(comment)
		return '' unless logged_in?
		if current_user.admin? || current_user == comment.user || current_user == comment.object.user
			return "<input type='checkbox' name='comments[]' value='#{comment.id}' />"
		else
			return ''
		end
	end
end
